package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.dto.BaseDTO;
import com.xingkeduo.pojo.dto.ConsumerDTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;

import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/4/22 20:14
 **/
public class ConsumerDTOJTO extends BaseJson {


	
    public static BaseDTO createQueryForPrimary(String json) throws Throwable {
        BaseDTO query = new BaseDTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setId(validString(root,"companyid",20, YesNoEnum.NO));

        return query;
    }

    /**
     * 返回实体信息
     *
     * @param table:
     * @return : com.fasterxml.jackson.databind.node.ObjectNode
     * @author : gaozifeng
     * @date : 2019年4月23日10:54:28
     */
    public static ObjectNode toJson(ConsumerDTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        if (table != null) {

            root.put("id", table.getId());
            root.put("name", table.getName());
            root.put("phone", table.getPhone());
        }
        return root;
    }

    public static ArrayNode toJson(List<ConsumerDTO> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<ConsumerDTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJson(iterator.next()));
            }

        return root;
    }
}
